package com.test.pudamoney.activity_fragment_pudamoney.loan_pudamoney;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.core.content.ContextCompat;
import androidx.fragment.app.FragmentTransaction;

import com.test.pudamoney.R;
import com.test.pudamoney.activity_fragment_pudamoney.ApplyPermissionActivity_pudamoney;
import com.test.pudamoney.activity_fragment_pudamoney.base_pudamoney.BaseLoanFragment_pudamoney;
import com.test.pudamoney.activity_fragment_pudamoney.fragment_pudamoney.DataSafetyFragment_pudamoney;
import com.test.pudamoney.activity_fragment_pudamoney.fragment_pudamoney.LoanFailFragment_pudamoney;
import com.test.pudamoney.activity_fragment_pudamoney.fragment_pudamoney.LoanMainFragment_pudamoney;
import com.test.pudamoney.activity_fragment_pudamoney.fragment_pudamoney.LoanWaitFragment_pudamoney;
import com.test.pudamoney.activity_fragment_pudamoney.fragment_pudamoney.PersonalCenterFragment_pudamoney;
import com.test.pudamoney.activity_fragment_pudamoney.fragment_pudamoney.RepayFragment_pudamoney;
import com.test.pudamoney.bean_pudamoney.config_info_pudamoney.AppShowInfo_pudamoney;
import com.test.pudamoney.bean_pudamoney.process_pudamoney.ProcessInfo_pudamoney;
import com.test.pudamoney.constant_pudamoney.Constant_pudamoney;
import com.test.pudamoney.dialog_pudamoney.InitOfflinePayChannelDialog_pudamoney;
import com.test.pudamoney.util_pudamoney.PermissionUtil_pudamoney;
import com.test.pudamoney.util_pudamoney.StringUtil_pudamoney;
import com.test.pudamoney.util_pudamoney.http_pudamoney.HttpManager_pudamoney;
import com.test.pudamoney.activity_fragment_pudamoney.base_pudamoney.BaseLoanActivity_pudamoney;
import com.test.pudamoney.activity_fragment_pudamoney.info_pudamoney.InfoUploadActivity_pudamoney;

import java.util.Objects;

public class LoanMainActivity_pudamoney extends BaseLoanActivity_pudamoney {

    private RelativeLayout fragmentLayout;
    private ViewGroup homeLayout, dataSafetyLayout, personalCenterLayout;
    private ImageView homeIv, dataSafetyIv, personalCenterIv;
    private TextView homeTv, dataSafetyTv, personalCenterTv;

    private int fragmentShow = 1;

    private BaseLoanFragment_pudamoney homeFragment;
    private DataSafetyFragment_pudamoney dataSafetyFragment;
    private PersonalCenterFragment_pudamoney personalCenterFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_loan_main_pudamoney);
        init();
    }

    private void init(){
        fragmentLayout = findViewById(R.id.activity_loan_main_layout_fragment);
        homeLayout = findViewById(R.id.activity_loan_main_layout_home);
        homeIv = findViewById(R.id.activity_loan_main_iv_home);
        homeTv = findViewById(R.id.activity_loan_main_tv_home);
        dataSafetyLayout = findViewById(R.id.activity_loan_main_layout_data_safety);
        dataSafetyIv = findViewById(R.id.activity_loan_main_iv_data_safety);
        dataSafetyTv = findViewById(R.id.activity_loan_main_tv_data_safety);
        personalCenterLayout = findViewById(R.id.activity_loan_main_layout_personal_center);
        personalCenterIv = findViewById(R.id.activity_loan_main_iv_personal_center);
        personalCenterTv = findViewById(R.id.activity_loan_main_tv_personal_center);

        dataSafetyFragment = new DataSafetyFragment_pudamoney();
        personalCenterFragment = new PersonalCenterFragment_pudamoney(this);

        homeLayout.setOnClickListener(v -> changeToHome());
        dataSafetyLayout.setOnClickListener(v -> changeToDataSafety());
        personalCenterLayout.setOnClickListener(v -> changeToPersonalCenter());

    }

    @Override
    protected void onResume() {
        super.onResume();
        updatePage();
    }

    private void updatePage() {

        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();

        if (homeFragment != null) fragmentTransaction.remove(homeFragment);
        if (dataSafetyFragment != null) fragmentTransaction.remove(dataSafetyFragment);
        if (personalCenterFragment != null) fragmentTransaction.remove(personalCenterFragment);

        if (ProcessInfo_pudamoney.getInstance().isOrderPendingRepayment() || ProcessInfo_pudamoney.getInstance().isOrderOverdueRepayment()) {//订单状态-待还款
            homeFragment = new RepayFragment_pudamoney();
        } else if (ProcessInfo_pudamoney.getInstance().isOrderLoaning() || ProcessInfo_pudamoney.getInstance().isLoanReviewing()) {//订单状态-放款中/审核中
            homeFragment = new LoanWaitFragment_pudamoney();
        } else if (ProcessInfo_pudamoney.getInstance().isLoanRejcted()) {//订单状态-被拒绝
            homeFragment = new LoanFailFragment_pudamoney();
        } else {
            homeFragment = new LoanMainFragment_pudamoney();
        }

        fragmentTransaction.add(R.id.activity_loan_main_layout_fragment, Objects.requireNonNull(homeFragment))
                .add(R.id.activity_loan_main_layout_fragment, dataSafetyFragment)
                .add(R.id.activity_loan_main_layout_fragment, personalCenterFragment);

        switch (fragmentShow) {
            case 2:
                fragmentTransaction.show(dataSafetyFragment).hide(homeFragment).hide(personalCenterFragment);
                break;
            case 3:
                fragmentTransaction.show(personalCenterFragment).hide(homeFragment).hide(dataSafetyFragment);
                break;
            default:
                fragmentTransaction.show(homeFragment).hide(dataSafetyFragment).hide(personalCenterFragment);
        }

        fragmentTransaction.commit();
    }

    private void changeToHome() {

        homeIv.setImageResource(R.drawable.icon_tab_home_true);
        dataSafetyIv.setImageResource(R.drawable.icon_tab_data_safety_false);
        personalCenterIv.setImageResource(R.drawable.icon_tab_personal_center_false);

        homeTv.setTextColor(ContextCompat.getColor(this,R.color.blue_pudamoney));
        dataSafetyTv.setTextColor(ContextCompat.getColor(this,R.color.gray_dd));
        personalCenterTv.setTextColor(ContextCompat.getColor(this,R.color.gray_dd));

        getSupportFragmentManager().beginTransaction().show(homeFragment).hide(dataSafetyFragment).hide(personalCenterFragment).commit();

        fragmentShow = 1;
    }

    private void changeToDataSafety() {

        homeIv.setImageResource(R.drawable.icon_tab_home_false);
        dataSafetyIv.setImageResource(R.drawable.icon_tab_data_safety_true);
        personalCenterIv.setImageResource(R.drawable.icon_tab_personal_center_false);

        homeTv.setTextColor(ContextCompat.getColor(this,R.color.gray_dd));
        dataSafetyTv.setTextColor(ContextCompat.getColor(this,R.color.blue_pudamoney));
        personalCenterTv.setTextColor(ContextCompat.getColor(this,R.color.gray_dd));

        getSupportFragmentManager().beginTransaction().show(dataSafetyFragment).hide(homeFragment).hide(personalCenterFragment).commit();

        fragmentShow = 2;
    }

    private void changeToPersonalCenter() {

        homeIv.setImageResource(R.drawable.icon_tab_home_false);
        dataSafetyIv.setImageResource(R.drawable.icon_tab_data_safety_false);
        personalCenterIv.setImageResource(R.drawable.icon_tab_personal_center_true);

        homeTv.setTextColor(ContextCompat.getColor(this,R.color.gray_dd));
        dataSafetyTv.setTextColor(ContextCompat.getColor(this,R.color.gray_dd));
        personalCenterTv.setTextColor(ContextCompat.getColor(this,R.color.blue_pudamoney));

        getSupportFragmentManager().beginTransaction().show(personalCenterFragment).hide(homeFragment).hide(dataSafetyFragment).commit();

        fragmentShow = 3;
    }

    //配置信息返回响应
    public void onAppShowInfoResponse() {
        if (personalCenterFragment != null) personalCenterFragment.updateCustomerService();
    }

    //额度列表返回响应
    public void onLoanLimitInfoResponse() {
        if (homeFragment != null && homeFragment instanceof LoanMainFragment_pudamoney)
            ((LoanMainFragment_pudamoney) homeFragment).updateLoanAmount_pudamoney();
    }

    @Override
    public void onUserProcessResponse_pudamoney() {
        //判断权限状态，如果存在未获取的权限则跳转到权限申请页
        if (!PermissionUtil_pudamoney.checkLaunchPermissions(this, AppShowInfo_pudamoney.getInstance().getHome())) {
            startActivityForSingleTop_pudamoney(ApplyPermissionActivity_pudamoney.class);
            return;
        }
        //订单状态-待还款
        if (ProcessInfo_pudamoney.getInstance().isOrderPendingRepayment() || ProcessInfo_pudamoney.getInstance().isOrderOverdueRepayment()) {
            if (homeFragment instanceof RepayFragment_pudamoney) {
                HttpManager_pudamoney.getInstance().getRepayDetail();
            } else updatePage();
            return;
        }
        //订单状态-审核中/放款中
        if (ProcessInfo_pudamoney.getInstance().isLoanReviewing() || ProcessInfo_pudamoney.getInstance().isOrderLoaning()) {
            if (!(homeFragment instanceof LoanWaitFragment_pudamoney)) updatePage();
            return;
        }
        //订单状态-被拒绝
        if (ProcessInfo_pudamoney.getInstance().isLoanRejcted()) {
            if (!(homeFragment instanceof LoanFailFragment_pudamoney)) updatePage();
            return;
        }
        //其他订单状态
        if (homeFragment instanceof LoanMainFragment_pudamoney) {
            if (((LoanMainFragment_pudamoney) homeFragment).isRefresh){
                HttpManager_pudamoney.getInstance().queryLoanLimit();
                ((LoanMainFragment_pudamoney) homeFragment).isRefresh = false;
            }
            else startLoanProcess();
        } else {
            updatePage();
        }
    }

    //借款流程
    private void startLoanProcess() {

        if (ProcessInfo_pudamoney.getInstance().getStepsSize()>0){
            startActivity_pudamoney(InfoUploadActivity_pudamoney.class);
        }

        else {//如果不是处理还款流程、状态流程、认证流程，直接去借款确认页
            startActivity_pudamoney(LoanApplyActivity_pudamoney.class);
            HttpManager_pudamoney.getInstance().requestLoanApplyDetail();
        }

    }

    public void onLoanApplyDetailResponse() {

        //订单状态-放款中/审核中
        if (ProcessInfo_pudamoney.getInstance().isOrderLoaning() || ProcessInfo_pudamoney.getInstance().isLoanReviewing()) {
            if ((homeFragment instanceof LoanWaitFragment_pudamoney)) ((LoanWaitFragment_pudamoney) homeFragment).onLoanApplyDetailResponse();
        }else startActivity_pudamoney(LoanApplyActivity_pudamoney.class);
    }

    public void onRepayDetailResponse() {
        if (homeFragment != null && homeFragment instanceof RepayFragment_pudamoney)
            ((RepayFragment_pudamoney) homeFragment).updateRepayDetail();
    }

    //获取bankAccountList返回(尼日)
    public void onFetchBankListResponse_Niri(){
        //订单状态-还款中
        if (ProcessInfo_pudamoney.getInstance().isOrderPendingRepayment() || ProcessInfo_pudamoney.getInstance().isOrderOverdueRepayment()) {
            if (homeFragment instanceof RepayFragment_pudamoney) ((RepayFragment_pudamoney) homeFragment).onFetchBankListResponse_Niri();
        }
    }

    //获取线下还款通道响应-尼日
    public void onInitOfflinePayChannelResponse() {
        InitOfflinePayChannelDialog_pudamoney.showDialog(this, dialogInterface -> HttpManager_pudamoney.getInstance().getRepayDetail());
    }

}